﻿@page "/ContentEdit"
@layout EmptyLayout
@inject HttpClient Http
@inject NavigationManager navigation
<style>
    .caption { width: 120px; }
    .title { width: 100%; }
    #edui1_iframeholder { height: 500px;}
</style>

<table width="100%">
    <tr>
        <td class="caption">标题：</td>
        <td colspan="3"><input class="title" @bind="model.Title" /></td>
    </tr>
    <tr>
        <td class="caption">所属分类Id：</td>
        <td><input style="width:200px;" @bind="model.CategoryId" /></td>
        <td class="caption">发生时间：</td>
        <td><DatePicker style="width:200px;" TValue="DateTime?" Value="model.RealTime"></DatePicker></td>
    </tr>

    <tr>
        <td class="caption">正文内容：</td>
        <td colspan="3"><UEditor.Editor @ref="editor" @bind-Value="model.MainBody" @bind-Html="html" Height="500px" Width="100%" /></td>
    </tr>
    <tr>
        <td class="caption"></td>
        <td colspan="3"><button style="z-index:999;" @onclick="save">保存</button></td>
    </tr>

</table>


@code {
    Model.Content model = new Model.Content();
    string value = "";
    string html;

    UEditor.Editor editor;

    protected override async Task OnInitializedAsync()
    {
        int id = navigation.getQueryInt("id");
        if (id > 0)
        {
            string pms = string.Format("id={0}", id);
            string url = Tools.get_api_url("content", "get_by_id", pms);
            string content_data = await Http.GetStringAsync(url);
            model = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Content>(content_data);
        }
        else
        {
            model.CategoryId = navigation.getQueryInt("pid");
            model.RealTime = DateTime.Now;
        }
    }

    void save()
    {
        if (model.Id <= 0)
        {
            model.CreateTime = System.DateTime.Now;
        }

        model.MainBody = editor.Value;
        model.ModifyTime = DateTime.Now;
        string function = model.Id > 0 ? "update" : "insert";
        string post_url = Tools.get_api_url("content", function);
        string post_data = Newtonsoft.Json.JsonConvert.SerializeObject(model);

        var httpRequestMessage = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri(post_url),
            Content = new StringContent(post_data, System.Text.Encoding.UTF8, "application/json")
        };
        var httpReponseMessage = Http.SendAsync(httpRequestMessage);
        
    }
}
